package com.robot.orders.controller;

import com.alibaba.fastjson.JSON;
import com.robot.orders.pojo.Confirmation;
import com.robot.orders.service.ConfirmationService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * confirmation控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/confirmation")
public class ConfirmationJsonController {
    @Autowired
    private ConfirmationService confirmationService;

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findConfirmationsBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindConfirmationsBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Confirmation> pageList = confirmationService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<Confirmation>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据agentId查询采购付款凭证列表
     *
     * @param agentId
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findConfirmationsByAgentIdOrderByCreatedAt/{agentId}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindConfirmationsByAgentIdOrderByCreatedAt(@PathVariable String agentId, @PathVariable int page, @PathVariable int size) {
        Page<Confirmation> pageData = confirmationService.findAllByAgentIdAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 根据agentId和status查询采购付款凭证列表
     *
     * @param agentId
     * @param status
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "third/findConfirmationsByAgentIdAndStatusOrderByCreatedAt/{agentId}/{status}/{page}/{size}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindConfirmationsByAgentIdAndStatusOrderByCreatedAt(@PathVariable String agentId, @PathVariable String status, @PathVariable int page, @PathVariable int size) {
        Page<Confirmation> pageData = confirmationService.findAllByAgentIdAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(agentId, status, page, size);
        return JSON.toJSONString(new PageResult<>(pageData.getTotalElements(), pageData.getContent()));
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "third/findConfirmationById/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindConfirmationById(@PathVariable String id) {
        return JSON.toJSONString(confirmationService.findById(id));
    }

    /**
     * 增加采购付款凭证
     *
     * @param confirmation
     */
    @RequestMapping(value = "third/addConfirmation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public Result thirdAddConfirmation(@RequestBody String confirmation) {
        Confirmation confirmation1 = JSON.parseObject(confirmation, Confirmation.class);
        confirmationService.add(confirmation1);
        return new Result(StatusCode.SUCCESS_DELETED);
    }

    /**
     * 修改
     *
     * @param confirmation
     */
    @RequestMapping(value = "third/updateConfirmation", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdateConfirmation(@RequestBody String confirmation) {
        confirmationService.update(JSON.parseObject(confirmation, Confirmation.class));
    }
}
